package com.comleader.operator.common.response;

import static com.comleader.operator.common.response.ResultEnum.FAILED;
import static com.comleader.operator.common.response.ResultEnum.SUCCESS;

import java.io.Serializable;

import lombok.Data;

/**
 * 功能描述: 统一API响应结果封装
 * 
 * @Author: 连新景
 * @Date: 2024/7/25 9:09
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 3643242578736827459L;

    /**
     * 返回状态码（0成功,其它为失败）
     */
    private int code;
    /**
     * 返回描述
     */
    private String msg;
    /**
     * 返回数据
     */
    private T data;

    private static <T> Result<T> result(T data, int code, String msg) {
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> success() {
        return result(null, SUCCESS.getCode(), SUCCESS.getMsg());
    }

    public static <T> Result<T> success(T data) {
        return result(data, SUCCESS.getCode(), SUCCESS.getMsg());
    }

    public static <T> Result<T> success(T data, ResultEnum resultEnum) {
        return result(data, SUCCESS.getCode(), resultEnum.getMsg());
    }

    public static <T> Result<T> fail() {
        return result(null, FAILED.getCode(), FAILED.getMsg());
    }

    public static <T> Result<T> fail(ResultEnum resultEnum) {
        return result(null, resultEnum.getCode(), resultEnum.getMsg());
    }

    public static <T> Result<T> fail(Integer code, String msg) {
        return result(null, code, msg);
    }

    public static <T> Result<T> fail(T data) {
        return result(data, FAILED.getCode(), FAILED.getMsg());
    }

    public static <T> Result<T> fail(T data, String msg) {
        return result(data, FAILED.getCode(), msg);
    }

    public static <T> Result<T> fail(String msg) {
        return result(null, FAILED.getCode(), msg);
    }
}
